const {
  $util,
} = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    checkdesc: '', // 驳回理由
    isShowReason: false, // 是否显示驳回理由
    isdo: '',
    // datachecknews资讯 upload志愿服务 multitap关系转接 learnrecord中心组学习记录 secretarymail党风廉政书记信箱 career公益事业 offrecord线下培训 
    // 驳回、通过21       驳回、通过32    驳回、通过23      驳回、通过23             驳回、通过21                  驳回、通过21    驳回、通过23
    kv: {
      promisenew: '承诺审批', 
      secretarymail: '建议审批',
      offrecord: '审批详情',
    },
  },
  onShow() {
    let wxData = wx.getStorageSync('qyUserInfo') || {};
    this.setData(wxData);
    this.setData({
      isSaaS: wx.getStorageSync('isSaaS'),
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(opts) {
    this.setData(opts);
    this.loadDetail();
    opts.title && wx.setNavigationBarTitle({
      // title: this.data.kv[opts.isdo] || opts.title,
      title: opts.title,
    });
  },
  loadDetail() {
    const {
      id,
      isdo,
    } = this.data;
    $util.request({
      url: `entry/wxapp/verify.${isdo}.getDetail`,
      data: {
        id,
      },
      success: (res) => {
        this.setData({
          detail: res.item,
        });
      },
    });
  },
  btnprevent: false,
  submit(e) {
    this.setData({
      isShowReason: false,
    });
    let {
      ischeck,
      ischeckdesc,
    } = e.currentTarget.dataset;
    const {
      id,
      isdo,
      checkdesc,
      ischeckVal = '',
    } = this.data;
    (ischeckdesc === '1') && (ischeck = ischeckVal);
    let param = {
      id: id,
    };
    wx.showModal({
      content: `确定要${ischeck === '2' ? '驳回' : '通过'}吗？`,
      success: (res) => {
        if (res.confirm) {
          if (this.btnprevent) return;
          this.btnprevent = true;
          if (ischeck === '2') param.checkdesc = checkdesc;
          if (isdo === 'datachecknews' || isdo === 'secretarymail' || isdo === 'career' || isdo === 'investigate' || isdo === 'promisenew') {
            param.check = ischeck === '2' ? 2 : 1;
          }
          if (isdo === 'upload') {
            param.check = ischeck === '2' ? 3 : 2;
          }
          if (isdo === 'multitap' || isdo === 'learnrecord' || isdo === 'offrecord') {
            param.check = ischeck === '2' ? 2 : 3;
          }
          if (isdo === 'multitap') {
            param.remark = checkdesc;
          }
          $util.request({
            url: `entry/wxapp/verify.${isdo}.verify`,
            data: param,
            success: () => {
              wx.showToast({
                title: '操作成功',
                icon: 'success',
                success: () => {
                  setTimeout(() => {
                    wx.navigateBack();
                  }, 1000);
                },
              });
            },
            complete: () => {
              this.btnprevent = false;
            },
          });
        }
      },
    });
  },
  // 显示理由
  showReason(e) {
    this.setData({
      isShowReason: !this.data.isShowReason,
      ischeckVal: e.currentTarget.dataset.ischeck,
    });
  },
  bindInpVal(e) {
    const typ = e.currentTarget.dataset.typ;
    this.setData({
      [typ]: e.detail.value,
    });
  },
  // 预览图片
  toPreviewImg(e) {
    const { img } = this.data.detail;
    const { idx } = e.currentTarget.dataset;
    wx.previewImage({
      urls: img,
      current: img[idx],
    });
  },
});